package top.anydata.lifeofcode.lifesystem.util;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.Random;

/**
 * 功能描述: 基础相关工具类
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2024/2/6 3:45 PM
 */
@Slf4j
public class CommonUtils {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    private static final Random RANDOM = new Random();

    /**
    * 功能描述: 生成指定长度的验证码
    * @author: <a href="mailto:caingao@anydata.top">CainGao</a>
    * @date: 4:42 PM 2024/2/18
    * @param: [length]
    * @return: java.lang.String
    */
    public static String generateCaptcha(int length) {
        StringBuilder captcha = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            captcha.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
        }
        return captcha.toString();
    }

    public static String generateCatchaImg(String captcha){
        int width = 150;
        int height = 50;

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufferedImage.createGraphics();

        Font font = new Font("Georgia", Font.PLAIN, 24); // 使用普通样式，增大字体大小
        g2d.setFont(font);

        g2d.setColor(new Color(200, 200, 255)); // 设置背景颜色为浅蓝色
        g2d.fillRect(0, 0, width, height);

        g2d.setColor(new Color(0, 0, 0)); // 设置字体颜色为黑色
        int x = 10;
        int y = 30;
        for (int i = 0; i < captcha.length(); i++) {
            g2d.drawString(captcha.charAt(i) + "", x, y);
            x += 20; // 每个字符之间的间距为20
        }
        g2d.dispose();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            return Base64.getEncoder().encodeToString(imageInByte);
        } catch (Exception e) {
            log.error("生成验证码图片失败", e);
            throw new RuntimeException(e);
        }
    }



    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
    private static final String SECRET_KEY = "1234567890123456"; // 16位密钥

    /**
    * 功能描述 : AES 加密
    * @author: <a href="mailto:caingao@anydata.top">CainGao</a>
    * @date: 4:48 PM 2024/2/18
    * @param: [data]
    * @return: java.lang.String
    */
    public static String encrypt(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    /**
    * 功能描述: AEC 解密
    * @author: <a href="mailto:caingao@anydata.top">CainGao</a>
    * @date: 4:48 PM 2024/2/18
    * @param: [data]
    * @return: java.lang.String
    */
    public static String decrypt(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(data));
        return new String(decryptedBytes);
    }



}
